﻿// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SoftReference.generated.h"

/**	C++ 中资源的软引用
 *	间接引用并不存放资源本身，本章节主要介绍 FSoftObjectPath、FSoftClassPath、TSoftObjectPtr
 *	使用用这种方式需要手动加载资源（同步/异步加载：LoadObject, StaticLoadObject, FStreamingManager）
 *	FSoftObjectPath
 *		FSoftObjectPath 是一个简单的结构体，其中有一个字符串包含资源的完整名称。
 *		FSoftObjectPath.SolveObject() 可以检查其引用的资源是否已经载入在内存中，若载入则返还资源对象指针，否则返还空。
 *		FSoftObjectPath.Reset() 重置软引用为空
 *		AllowedClasses meta标签可以筛选资源类型
 *			筛选自定义类 类型时，只能指定放置在 level 中的物体实例，不推荐
 *	FSoftClassPath
 *		FSoftClassPath 继承自 FSoftObjectPath，用于存储一个类型的软引用
 *		MetaClass meta标签可以筛选类类型
 *		FSoftClassPath 继承自 FSoftObjectPath，因此用法与 FSoftObjectPath 差不多
 *	TSoftObjectPtr<T>
 *		TSoftObjectPtr 基本上是包含了 FSoftObjectPath 的 TWeakObjectPtr，是智能指针的一种
 *		TSoftObjectPtr 与蓝图中的 SoftObjectReference 是一回事
 *		可用于在异步加载完成触发回调函数时，获取资源对应的对象指针
 *		TSoftObjectPtr.IsPending() 方法可检查资源是否已准备好可供访问
 *		TSoftObjectPtr.Get() 如果被引用资源存在于内存中，将返回这个资源
 *	TSoftClassPtr<T>
 *		获取类的软引用，转成 UClass*
 **/

UCLASS()
class MISCELLANEOUSPROJECT_API ASoftReference : public AActor
{
	GENERATED_BODY()

public:
	// FSoftObjectPath
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObject", meta = (AllowedClasses = "SkeletalMesh, StaticMesh" ))
	FSoftObjectPath SoftObjectPath1;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObject", meta = (AllowedClasses = "Texture2D"))
	FSoftObjectPath SoftObjectPath2;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObject", meta = (AllowedClasses = "Blueprint Class"))
	FSoftObjectPath SoftObjectPath3;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObject", meta = (AllowedClasses = "Drone")) //自定义类型 不推荐
	FSoftObjectPath SoftObjectPath4;

	// FSoftClassPath
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectClass")
	FSoftClassPath SoftClassPath;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectClass", meta = ( MetaClass= "Pawn"))
	FSoftClassPath SoftClassPath_Pawn;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectClass", meta = (MetaClass = "Drone"))
	FSoftClassPath SoftClassPath_Drone;

	// TSoftObjectPtr<T>
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectPtr")
	TSoftObjectPtr<UObject> SoftObjectPtr1;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectPtr")
	TSoftObjectPtr<UObject> SoftObjectPtr2;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftObjectPtr")
	TSoftObjectPtr<UTexture2D> SoftObjectPtr_Texture2D;

	// TSoftClassPtr<T>
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftClassPtr")
	TSoftClassPtr<AActor> SoftClassPtr_Actor;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftClassPtr")
	TSoftClassPtr<APawn> SoftClassPtr_Pawn;

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SoftClassPtr")
	TSoftClassPtr<UUserWidget> SoftClassPtr_UserWidget;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
};
